package com.xs.shop.orders.service.controller;

import com.xs.shop.common.Result;
import com.xs.shop.orders.model.dto.CartDTO;
import com.xs.shop.orders.model.entity.Cart;
import com.xs.shop.orders.service.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs,zyx
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/cart")
@Tag(name = "购物车管理", description = "购物车的CRUD操作")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("")
    @Operation(summary = "新增购物车项", description = "添加商品到购物车")
    public Result<Object> addCart(@RequestBody CartDTO cartDTO) {
        cartService.addCart(cartDTO);
        return Result.success();
    }

    @PutMapping("ruduceCart")
    @Operation(summary = "减少购物车项", description = "减少购物车中的商品")
    public Result<Object> ruduceCart(@RequestBody CartDTO cartDTO) {
        cartService.ruduceCart(cartDTO);
        return Result.success();
    }

    @DeleteMapping("")
    @Operation(summary = "清空购物车", description = "根据MemberID清空购物车")
    public Result<Boolean> deleteCart() {
        boolean result = cartService.deleteCart();
        return Result.success(result);
    }

    @PostMapping("updateCart")
    @Operation(summary = "更新购物车项", description = "更新购物车项信息")
    public Result<Cart> updateCart(@RequestBody CartDTO cartDTO) {
        Cart updatedCart = cartService.updateCart(cartDTO);
        return Result.success(updatedCart);
    }

    @GetMapping("getCart")
    @Operation(summary = "获取会员购物车", description = "根据会员ID获取购物车列表")
    public Result<List<Cart>> getCart() {
        List<Cart> cartList = cartService.getCart();
        return Result.success(cartList);
    }
}

